package wpp.view;

import java.util.List;

import org.apache.commons.lang.math.RandomUtils;

import javafx.animation.ParallelTransitionBuilder;
import javafx.animation.RotateTransitionBuilder;
import javafx.animation.TranslateTransitionBuilder;
import javafx.util.Duration;

import wpp.cfg.DataConfig;
import wpp.cfg.WPFileBean;

public class FavorPreviewPane extends PreviewPane {
	
	private static final Double ANIMATED_START_POS_Y = 800.0;
	private static final Double ANIMATED_DURATION_MILLIS = 300.0;
	private static final Double ANIMATED_ROTATE_ANGLE = 720.0;
	
	public FavorPreviewPane() {
		
	}
	
	public void cleanImages() {
		imageShowContainer.getChildren().clear();
	}
	
	public void loadImages() {
		List<WPFileBean> wpFileBeans = DataConfig.getFavoriteWpFileBeans(GRID_CAPACITY, true);
		for(int i = 0; i < wpFileBeans.size(); i++) {
			SimpleImageView imageView = new SimpleImageView(wpFileBeans.get(i));
			imageView.setLayoutX((PREF_HEIGHT - imageView.getImage().getWidth()) / 2);
			imageView.setLayoutY(ANIMATED_START_POS_Y);
			imageShowContainer.getChildren().add(imageView);
			Double randowmRotate = RandomUtils.nextInt(30) - 15.0;
			ParallelTransitionBuilder.create().children(
				TranslateTransitionBuilder.create().toX(POSS[i][0] - imageView.getLayoutX())
					.toY(POSS[i][1] - imageView.getLayoutY())
					.duration(Duration.millis(ANIMATED_DURATION_MILLIS)).build(),
				RotateTransitionBuilder.create().toAngle(ANIMATED_ROTATE_ANGLE + randowmRotate)
					.duration(Duration.millis(ANIMATED_DURATION_MILLIS)).build()
			).node(imageView).delay(Duration.millis(i * ANIMATED_DURATION_MILLIS)).build().play();	
		}
	}	
}
